package com.juc.future;

import java.util.concurrent.*;

/**
 * @Author Zan
 * @Create 2024/5/6 22:33
 * @ClassName: CompletableFutureAPIDemo
 * @Description : TODO 请用一句话描述该类的功能
 */
public class CompletableFutureAPI2Demo {
    public static void main(String[] args) {

        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 1;
        }, threadPool).handle((f, e) -> {
            System.out.println("222");
            int i = 10/0;
            return f + 2;
        }).handle((f,e) -> {
            System.out.println("333");
            return f + 3;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("-----计算结果：" + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        });

        System.out.println(Thread.currentThread().getName() + "--------主线程先去忙其他任务");

        threadPool.shutdown();
    }
}
